package com.sample.javabean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.sample.action.form.RightForm;

import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;

public class Right {

	private String role_no;//角色编号
	private String right_no;//权限编号
	private String right_mean;//权限说明
	private String if_user;//是否能使用该权限

	private JdbcUtils jdbcUtils = new JdbcUtils();


	//构造函数
	public Right() {
		super();
	}


	public Right(String role_no, String right_no, String right_mean, String if_user) {
		super();
		this.role_no = role_no;
		this.right_no = right_no;
		this.right_mean = right_mean;
		this.if_user = if_user;
	}

	/**
	 * 添加权限
	 */
	public void addRight(RightForm form)throws Exception{
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "insert into t_right(role_no,right_no,right_mean,if_user) values('"
					+ form.getRole_no() + "','" +form.getRight_no()+"','"+form.getRight_mean()+"','"+1+"')";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);


			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception("添加权限失败，Exception_bean:<--" + e+"->");
		} finally {
			jdbcUtils.close(conn, stmt);
		}
	}
	/**
	 * 删除权限
	 */
	public void delRight()throws Exception{
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "delete from t_right where role_no='"+this.role_no+"' and right_no='"+this.right_no+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {

			e.printStackTrace();
			throw new Exception("删除权限失败，Exception_bean:<--" + e+"->");
		} finally {
			jdbcUtils.close(conn, stmt);
		}
	}
	/**
	 * 修改权限
	 */
	public void editRight(RightForm form)throws Exception{
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			if(form!=null){
			// 3:构造插入SQL
			String sql = "update  t_right set right_mean='"+form.getRight_mean()+"'where right_no='"+form.getRight_no()+
					"'and role_no='"+form.getRole_no()+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);}
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception("编辑权限信息失败，Exception_bean:<--" + e+"->");
			
		} finally {
			jdbcUtils.close(conn, stmt);
		}
	}
	/**
	 * 加载权限信息
	 */
	public void load()throws Exception{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null; 
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "select * from  t_right where role_no='"+this.role_no+"' and right_no='"+this.right_no+"'";
			// 4:执行SQL并返回结果
			rs=stmt.executeQuery(sql);
			if(rs.next()){
			   this.role_no = rs.getString("role_no");
			   this.right_no = rs.getString("right_no");
			   this.right_mean = rs.getString("right_mean");
			   this.if_user = rs.getString("if_user");
			}
			// 5:关闭资源
		} catch (SQLException e) {
			
			e.printStackTrace();
			throw new Exception("加载权限信息失败！Exception_bean:<--"+e+"-->"); 
		} finally {
			jdbcUtils.close(conn, stmt);
		}	
	}
	
	/**
	 * 启动某权限
	 */
	public void on()throws Exception{
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "update  t_right set if_user='"+1+"'where right_no='"+this.right_no+
					"'and role_no='"+this.role_no+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception("开启权限失败，Exception_bean:<--"+e+"-->");
			
		} finally {
			jdbcUtils.close(conn, stmt);
		}
	}
	/**
	 * 关闭权限
	 */
	public void off()throws Exception{
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "update  t_right set if_user='"+0+"'where right_no='"+this.right_no+
					"'and role_no='"+this.role_no+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception("关闭权限失败，Exception_bean:<--"+e+"-->");
			
		} finally {
			jdbcUtils.close(conn, stmt);
		}	
	}
		/**
		 * 分页显示权限
		 */
		public Pager<Right> list(int page,int size,RightForm form) throws Exception{
			Connection conn = null;
			Statement stmt = null;
			ResultSet rs =null;
			Pager<Right> rights=new Pager<Right>();
			rights.setPage(page);
			rights.setSize(size);
			try {
			
				conn = jdbcUtils.getConn();
				stmt = conn.createStatement();
				int start=(page-1)*size;
				int limit=size;
				
				//查询SQL
				String selectSql="select * from t_right where 1=1";
				if(form!=null&&form.getRight_mean()!=null&&!"".equals(form.getRight_mean())){
					selectSql +=" and right_mean like'%"+form.getRight_mean()+"%'";
				}
				//统计SQL
				String countSql ="select count(*)as c from ("+selectSql+") as t1";
				//分页SQL
				 String pageSql=selectSql+" limit "+start+","+limit;
				 rs= stmt.executeQuery(countSql);
				 int total=0;
				 while(rs.next()){
					 total=rs.getInt("c");
				 }
				 rights.setTotal(total);
				 //查询分页信息
				 rs= stmt.executeQuery(pageSql);
				 while(rs.next()){
				
					 String role_no = rs.getString("role_no");
						String right_no = rs.getString("right_no");
						String right_mean= rs.getString("right_mean");
						String if_user = rs.getString("if_user");
						Right right=new Right(role_no, right_no, right_mean, if_user);
						rights.getDataSets().add(right);
				 }
				 rights.init();
			
			} catch (SQLException e) {
				throw new Exception("分页加载权限信息失败！");
			} finally {
				jdbcUtils.close(conn, stmt);
			}
			return rights;

		}


		//get、set方法
		public String getRole_no() {
			return role_no;
		}


		public void setRole_no(String role_no) {
			this.role_no = role_no;
		}


		public String getRight_no() {
			return right_no;
		}


		public void setRight_no(String right_no) {
			this.right_no = right_no;
		}


		public String getRight_mean() {
			return right_mean;
		}


		public void setRight_mean(String right_mean) {
			this.right_mean = right_mean;
		}


		public String getIf_user() {
			return if_user;
		}


		public void setIf_user(String if_user) {
			this.if_user = if_user;
		}
		
	
		
}
